package com.ruoyi.system.mapper;

import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.InvoiceRecords;
import com.ruoyi.system.domain.InvoiceRecordsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 开票明细Mapper接口
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
public interface InvoiceRecordsMapper 
{
    /**
     * 查询开票明细
     * 
     * @param id 开票明细主键
     * @return 开票明细
     */
    public InvoiceRecords selectInvoiceRecordsById(Long id);

    /**
     * 查询开票明细列表
     * 
     * @param invoiceRecords 开票明细
     * @return 开票明细集合
     */
    public List<InvoiceRecords> selectInvoiceRecordsList(InvoiceRecords invoiceRecords);

    public List<InvoiceRecordsVo> selectInvoiceProvideRecordsList(InvoiceRecords invoiceRecords);

    /**
     * 新增开票明细
     * 
     * @param invoiceRecords 开票明细
     * @return 结果
     */
    public int insertInvoiceRecords(InvoiceRecords invoiceRecords);

    /**
     * 修改开票明细
     * 
     * @param invoiceRecords 开票明细
     * @return 结果
     */
    public int updateInvoiceRecords(InvoiceRecords invoiceRecords);

    /**
     * 删除开票明细
     * 
     * @param id 开票明细主键
     * @return 结果
     */
    public int deleteInvoiceRecordsById(Long id);

    /**
     * 批量删除开票明细
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteInvoiceRecordsByIds(Long[] ids);

    public List<String> selectProductCode(@Param("list")List<String> productList);



//    @Select("select r.product_code,r.model,specs,r.product_price,r.plarn_price,r.plarn_price*sum(r.product_count)plarn_money, sum(r.product_money)price_money,sum(r.product_count)count,r.settlement_mode,r.product_name,r.product_brand,r.provide,r.company,r.status from invoice_records r  " +
//            "group by r.product_code,r.model,r.specs,r.product_price,r.settlement_mode,r.product_name,r.product_brand,r.provide,r.plarn_price,r.company,r.status ")

    @Select("<script>" +
            "select r.product_code,r.model,specs,r.product_price,r.plarn_price,r.product_price*sum(r.product_count)plarn_money, sum(r.plarn_price*r.product_count)price_money,sum(r.product_count)count,r.settlement_mode," +
            "r.product_name,r.product_brand,r.provide,r.company,r.status,g.product_group,r.create_time  from invoice_records r left join invoice_group g on r.product_code = g.product_code where 1=1 " +
            "<if test=\"productName!=null and productName!= ''\">" +
            " and r.product_name = #{productName} " +
            "</if>" +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and r.provide = #{provide} " +
            "</if>" +
            "<if test=\"company!=null and company!= ''\">" +
            " and r.company = #{company} " +
            "</if>" +
            "<if test=\"status!=null and status!= ''\">" +
            " and r.status = #{status} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and r.create_time = #{startTime} " +
            "</if>" +
            "<if test=\"productGroup!=null and productGroup!= ''\">" +
            " and g.product_group is NULL " +
            "</if>" +
            " group by r.product_code,r.model,r.specs,r.product_price,r.settlement_mode,r.product_name,r.product_brand,r.provide,r.plarn_price,r.company,r.status,g.product_group,date_format(r.create_time,'%Y-%m')" +
            " order by g.product_group" +
            "</script>")
    public List<Map<String,Object>>  listWebInfo(@Param("productName")String productName,
                                                 @Param("provide")String provide,
                                                 @Param("company")String company,
                                                 @Param("status")String status,@Param("productGroup")String productGroup,@Param("startTime")String startTime);


    @Select("<script>" +
            "select r.product_code,r.model,specs,r.plarn_price,sum(r.plarn_price*r.product_count)price_money,sum(r.product_count)count,r.settlement_mode," +
            "r.product_name,r.product_brand,r.provide,r.company,r.status,g.product_group,r.create_time  from invoice_records r left join invoice_group g on r.product_code = g.product_code where 1=1 " +
            "<if test=\"productName!=null and productName!= ''\">" +
            " and r.product_name = #{productName} " +
            "</if>" +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and r.provide = #{provide} " +
            "</if>" +
            "<if test=\"company!=null and company!= ''\">" +
            " and r.company = #{company} " +
            "</if>" +
            "<if test=\"status!=null and status!= ''\">" +
            " and r.status = #{status} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and r.create_time = #{startTime} " +
            "</if>" +
            "<if test=\"productGroup!=null and productGroup!= ''\">" +
            " and g.product_group is NULL " +
            "</if>" +
            " group by r.product_code,r.model,r.specs,r.product_price,r.settlement_mode,r.product_name,r.product_brand,r.provide,r.plarn_price,r.company,r.status,g.product_group,date_format(r.create_time,'%Y-%m')" +
            " order by g.product_group" +
            "</script>")
    public List<Map<String,Object>>  listProvideInfo(@Param("productName")String productName,
                                                 @Param("provide")String provide,
                                                 @Param("company")String company,
                                                 @Param("status")String status,@Param("productGroup")String productGroup,@Param("startTime")String startTime);


    @Select("<script>" +
            "select distinct(provide) from invoice_records where 1 = 1 "  +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "</script>")
    public List<String> listProvide(@Param("provide")String provide);

    @Select("select sum(plarn_price*product_count)sumMoney,settlement_mode,status from invoice_records where provide = #{provide} and  create_time = #{startTime}  group by settlement_mode,status")
    public List<Map<String,Object>> listSumMoney(@Param("provide")String provide,@Param("startTime")String startTime);


    @Select("select sum(r.plarn_price*r.product_count)groupMoney,r.company,g.product_group from  invoice_group g,invoice_records r  where  g.product_code = r.product_code" +
            " and r.provide = #{provide} and  r.create_time = #{startTime}  group by r.company,g.product_group")
    public List<Map<String,Object>> listGroupMoney(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("select sum(plarn_price*product_count)sumMoney,settlement_mode from invoice_records where provide = #{provide}  and  create_time = #{startTime}  and product_code  not in (select product_code from invoice_group) group by settlement_mode")
    public List<Map<String,Object>> listOtherMoney(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(r.plarn_price*r.product_count)groupMoney,r.company,g.product_group from invoice_group g,invoice_records r  where  g.product_code = r.product_code " +
            " and r.create_time = #{startTime}  " +
            "<if test=\"company!=null and company!= ''\">" +
            " and r.company = #{company} " +
            "</if>" +
            " group by r.company,g.product_group" +
            "</script>")
    public List<Map<String,Object>> listCompanyMoney(@Param("company")String company,@Param("startTime")String startTime);


    //封账
    @Update("update invoice_recoreds set status = '0' where provide= #{provide} and create_time = #{startTime}   ")
    public Integer   closingAccounts(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(product_money)allMoney from invoice_records where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectAllMoney(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(product_price*product_count)allMoney from invoice_records where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectMoney(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(plarn_price*product_count)allMoney from invoice_records where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectAllMoneyPrice(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(product_count)allCount from invoice_records where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectAllCount(@Param("provide")String provide,@Param("startTime")String startTime);


    @Select("<script>" +
            "select sum(product_count*product_price)sumMoney from invoice_records where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectSumProductMoney(@Param("provide")String provide,@Param("startTime")String startTime);


    @Select("<script>" +
            "select sum(product_money)inMoney,sum(product_price*product_count)outMoney from invoice_records where 1=1 " +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public List<Map<String,Object>>  selectSumProfitMoney(@Param("startTime")String startTime);

    @Select("<script>" +
            "update invoice_records set `status` = '1' " +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Integer updateStatus(@Param("startTime")String startTime);

    @Select("delete from invoice_records where create_time = #{time}")
    public Integer deleteUpload(@Param("time")String time);

    @Select("<script>" +
            "select sum(plarn_price*product_count)allMoney from invoice_records_audit where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectAllMoneyPriceForAudit(@Param("provide")String provide,@Param("startTime")String startTime);

    @Select("<script>" +
            "select sum(product_price*product_count)allMoney from invoice_records_audit where 1=1 " +
            "<if test=\"provide!=null and provide!= ''\">" +
            " and provide = #{provide} " +
            "</if>" +
            "<if test=\"startTime!=null and startTime!= ''\">" +
            " and create_time = #{startTime} " +
            "</if>" +
            "</script>")
    public Double  selectMoneyPriceForAudit(@Param("provide")String provide,@Param("startTime")String startTime);




}
